@echo off
chcp 936 >nul
echo ========================================
echo    E-commerce Microservices Starter - Batch Version
echo ========================================
echo.

:: Set project root directory
set PROJECT_ROOT=F:\shopping
echo Project Root: %PROJECT_ROOT%

:: Set Nacos server address
set NACOS_SERVER_ADDR=192.168.5.111:8848
set NACOS_GRPC_ADDR=192.168.5.111:9848
echo Nacos Server Address: %NACOS_SERVER_ADDR%

:: Check Java environment
echo Checking Java environment...
java -version
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: Java not found. Please install Java and set JAVA_HOME environment variable
    pause
    exit /b 1
)
echo Java environment check passed

:: Check Maven environment
echo Checking Maven environment...
mvn -version
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: Maven not found. Please install Maven and set MAVEN_HOME environment variable
    pause
    exit /b 1
)
echo Maven environment check passed

echo.
echo Compiling project...
echo.

:: Compile common-module
echo [1/6] Compiling common-module...
cd %PROJECT_ROOT%\common-module
call mvn clean install -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: common-module compilation failed
    pause
    exit /b 1
)
echo common-module compiled successfully

:: Compile auth-service
echo [2/6] Compiling auth-service...
cd %PROJECT_ROOT%\auth-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: auth-service compilation failed
    pause
    exit /b 1
)
echo auth-service compiled successfully

:: Compile user-service
echo [3/6] Compiling user-service...
cd %PROJECT_ROOT%\user-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: user-service compilation failed
    pause
    exit /b 1
)
echo user-service compiled successfully

:: Compile product-service
echo [4/6] Compiling product-service...
cd %PROJECT_ROOT%\product-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: product-service compilation failed
    pause
    exit /b 1
)
echo product-service compiled successfully

:: Compile order-service
echo [5/6] Compiling order-service...
cd %PROJECT_ROOT%\order-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: order-service compilation failed
    pause
    exit /b 1
)
echo order-service compiled successfully

:: Compile gateway-service
echo [6/6] Compiling gateway-service...
cd %PROJECT_ROOT%\gateway-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo ERROR: gateway-service compilation failed
    pause
    exit /b 1
)
echo gateway-service compiled successfully

echo.
echo All modules compiled successfully!
echo.
echo Starting services...
echo.

:: Start auth-service (port 8084)
echo Starting auth-service (port: 8084)...
start "auth-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\auth-service && java -jar target\auth-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: Start user-service (port 8081)
echo Starting user-service (port: 8081)...
start "user-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\user-service && java -jar target\user-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: Start product-service (port 8082)
echo Starting product-service (port: 8082)...
start "product-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\product-service && java -jar target\product-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: Start order-service (port 8083)
echo Starting order-service (port: 8083)...
start "order-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\order-service && java -jar target\order-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: Start gateway-service (port 8080)
echo Starting gateway-service (port: 8080)...
start "gateway-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\gateway-service && java -jar target\gateway-service-1.0.0.jar"

echo.
echo ========================================
echo    All services started successfully!
echo ========================================
echo.
echo Service Status:
echo - auth-service:    Running (port: 8084)
echo - user-service:    Running (port: 8081)
echo - product-service: Running (port: 8082)
echo - order-service:   Running (port: 8083)
echo - gateway-service: Running (port: 8080)

echo.
echo Access URLs:
echo - Gateway: http://localhost:8080
echo - Nacos Console: http://%NACOS_SERVER_ADDR%/nacos
echo - Auth Service: http://localhost:8084
echo - User Service: http://localhost:8081
echo - Product Service: http://localhost:8082
echo - Order Service: http://localhost:8083

echo.
echo Management Commands:
echo - View logs: Check the corresponding command window
echo - Stop service: Close the corresponding command window
echo - Restart: Run this script again

echo.
echo Press any key to exit this script (services will continue running)...
pause >nul